<#macro entrySelect schemaList>

<!-- Modal -->
<div class="modal" id="entry-select-modal" tabindex="-1" role="dialog" aria-labelledby="entry-select-Label" aria-hidden="true">
  <div id="entry-select-main" class="modal-dialog" style="width:400px" >
    <div class="modal-content" >
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="entry-select-Label">导入协议字段</h4>
      </div>
      <div class="modal-body" id="entry-select-modal-body" >
      		<form class="form-horizontal" role="form" >
			<div class="form-group">
				<label for="protcol" class="col-xs-3 control-label">协议:</label>
				<div class="col-xs-9">
					<select class=" form-control readbind writebind" id="protcol" name="protcol">
						<option value="">请选择</option>
						<#list schemaList as schema>  						
	    					<option value="${schema.schemaId}">${schema.schemaCode?trim}</option>    					
    					</#list>  
					</select>
				</div>
			</div>
			</form>
			<div class="panel panel-default" style="overflow:auto;margin:0;border:0" id="entrySelect-protTree-Div">
				<ul id="entrySelect-protTree" class="ztree" style="max-height:350px;scroll:auto"></ul>
			</div>
     		<div style="text-align: right;padding:10px 10px 0px 10px;">
        		<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>&nbsp;&nbsp;
        		<button type="button" class="btn btn-primary" id="entry-select-test">确定</button>
        	</div> 
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
$(function(){
	$('#entry-select-test').on('click',function(){
		var _entrySelect=entrySelect;
		_entrySelect.selectedData=_entrySelect.ztreeObj?_entrySelect.ztreeObj.getCheckedNodes(true):[];
		var protId=_entrySelect.$protcol.val(),protName=$('option[value='+protId+']',_entrySelect.$protcol).html();
		_entrySelect.onSure&&entrySelect.onSure(protName,_entrySelect.selectedData,_entrySelect.treeNode);
		_entrySelect.$modal.modal('hide');
	});
});

var entrySelect={
	$modal:$('#entry-select-modal'),
	onSure:function(nodes,treeNode){
		
	},
	show:function(treeNode){
		this.selectedData=[];
		this.treeNode=treeNode;
		this.$protcol.val("");
		this.ztreeObj&&this.ztreeObj.destroy();
		entrySelect.$modal.modal('show');
	},
	getValue:function(){
		return this.selectedData;
	},
	treeNode:{},
	selectedData:[],
	treeSetting:{
		check: {
			enable: true
		},
		view: {
			selectedMulti : false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: function(e, treeId, treeNode) {
				entrySelect.ztreeObj.checkNode(treeNode, !treeNode.checked, null, true);
				return false;
			}
		}
	},
	cacheData:{},
	$protcol:$('#protcol'),
	init:function(){
		var that=this;
		this.$protcol.on('change',function(){
			var protId=this.value,
				cacheData=that.cacheData;
			
			that.ztreeObj&&that.ztreeObj.destroy();
			
			if(!protId)
				return;
			
			if(!cacheData[protId])
				$.ajax(baseUrl+'/schema/getEntries.jhtml',{
						async:false,
						type:'POST',
						dataType:'json',
						data:{protId:protId}
					}).done(function(data){
						var nodeDatas=[];
						$.each(data,function(index,ele){
							nodeDatas.push($.extend({
								name:ele.entryName,
								id:ele.entryId,
								pId:ele.parentId,
								nocheck:ele.entryType==3||ele.entryType==4
							},ele));
						});
						cacheData[protId]=nodeDatas;
					}).fail(function(){});
			that.ztreeObj=$.fn.zTree.init($('#entrySelect-protTree'), that.treeSetting, cacheData[protId]);
			that.ztreeObj.expandAll(true);
		});
		return this;
	}
}.init();

</script>
</#macro>